#!/usr/bin/env bash
cmd=$( basename "$0" )
img="ts.knight:3.0.0.11.build1"
dockerName="ljs-root-docker"
gpu=false

function usage() {
    echo "Usage: ${cmd} [-i | -n | -h]" 
    echo "      启动docker"
    echo "      -i: <docker_img>:<docker_tag>, 缺省: ${img}"
    echo "      -n: docker容器名称，缺省: ${dockerName}"
    echo "      -g: docker中访问GPU"
    echo "      -h: 输出此信息"
    exit 1
}

shiftCnt=0
while getopts "i:n:gh" arg
do
    case "${arg}" in 
        i)
            img="${OPTARG}"
            shiftCnt=$(( ${shiftCnt} + 2 ))
            ;;
        n)
            dockerName="${OPTARG}"
            shiftCnt=$(( ${shiftCnt} + 2 ))
            ;;
        g)
            gpu=true
            shiftCnt=$(( ${shiftCnt} + 1 ))
            ;;
        *)
            usage
            ;;
    esac
done

shift ${shiftCnt}

echo run docker image ${img}
# 先删除旧的container
docker container rm -f ${dockerName} 2> /dev/null

if ${gpu}
then
    docker run --gpus all \
        --net host \
        --rm \
        --env NVIDIA_DRIVER_CAPABILITIES="utility,compute" \
        --name "${dockerName}" \
        -v "${HOME}/:/mnt/host_home" \
        -u root \
        -w /root \
        -it \
        ${img}
else
    docker run \
        --net host \
        --rm \
        --name "${dockerName}" \
        -v "${HOME}/:/mnt/host_home" \
        -u root \
        -w /root \
        -it \
        ${img}
fi
